﻿using EMS.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Linq;
using System.Windows;

namespace EMS.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class LoginViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        ///  AppData.Instance 是 AppData 类的静态单例实例（通过 Lazy<T> 或静态初始化实现）。
        public AppData AppData { get; private set; } = AppData.Instance;

        private string _userName;
        public string UserName
        {
            get => _userName;
            set
            {
                if (_userName != value)
                {
                    _userName = value;
                    RaisePropertyChanged(nameof(UserName));
                }
            }
        }

        private string _password;
        public string Password
        {
            get => _password;
            set
            {
                if (_password != value)
                {
                    _password = value;
                    RaisePropertyChanged(nameof(Password));
                }
            }
        }

        public LoginViewModel()
        {
            this.AppData.CurrentUser.Name = "123"; // 初始化当前用户
            this.AppData.CurrentUser.Password = "123"; // 初始化当前用户

        }
        /// <summary>
        ///登录
        /// </summary>
        public RelayCommand LoginCommand 
        {
            get
            {
                return new RelayCommand(() =>// 创建无参RelayCommand[1,7]
                {
                    // 1. 初始化数据访问层
                    MemberProvider memberProvider = new MemberProvider();
                    var list = memberProvider.Select();// 从数据库获取用户列表

                    // 2. 获取输入的用户凭证
                    var Username = AppData.CurrentUser.Name;// 从全局单例获取用户名
                    var pwd = AppData.CurrentUser.Password;// 获取密码
                    //  3. 执行用户查询（核心逻辑）
                    var user = list.FirstOrDefault(item =>
                    item.Name?.Trim().Equals(Username?.Trim(), StringComparison.OrdinalIgnoreCase) == true &&
                    item.Password?.Trim() == pwd?.Trim()
                    );

                    // if判断必须在Lambda内部
                    if (user == null)
                    {
                        MessageBox.Show("用户名或密码错误！");
                    }
                    else
                    {
                        MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                    }
                });  // Lambda结束

            }
        }

        public RelayCommand<Window> LoginCommand2
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    // 1. 初始化数据访问层
                    MemberProvider memberProvider = new MemberProvider();
                    var list = memberProvider.Select();// 从数据库获取用户列表

                    // 2. 获取输入的用户凭证
                    var Username = AppData.CurrentUser.Name;// 从全局单例获取用户名
                    var pwd = AppData.CurrentUser.Password;// 获取密码
                    //  3. 执行用户查询（核心逻辑）
                    var user = list.FirstOrDefault(item =>
                    item.Name?.Trim().Equals(Username?.Trim(), StringComparison.OrdinalIgnoreCase) == true &&
                    item.Password?.Trim().Equals(pwd?.Trim()) == true
                    );

                    // if判断必须在Lambda内部
                    if (user == null)
                    {
                        MessageBox.Show("用户名或密码错误！");
                    }
                    else
                    {
                        if (arg == null)
                        {
                            MessageBox.Show("登录窗口引用丢失。");
                            return;
                        }
                        MainWindow mainWindow = new MainWindow();
                        mainWindow.Show();
                        arg.Close(); // 关闭登录窗口
                    }
                });  // Lambda结束

            }
        }

    }
}